package com.qdaily.ui.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;
import com.qdaily.ui.R;

/**
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/9/15
 * Time: 17:28
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class ToastUtil {

    private static Toast toast;
    private static View toastView;
    private static TextView toastMsgView;
    private static Animation animation;

    public static void cancelToast() {
        if (toast == null) {
            return;
        }
        toast.cancel();
    }

    public static void showToast(Context context, String msg) {
        // 避免没有信息时，弹出无内容的黑框
        if (TextUtils.isEmpty( msg )) {
            return;
        }
        if (toast == null || toastMsgView == null) {
            toastView = LayoutInflater.from( context ).inflate( R.layout.view_toast, null );
            toast = new Toast( context );
            toast.setView( toastView );
            toastMsgView = (TextView) toastView.findViewById( R.id.message );
        }
        toastMsgView.setText( msg );
        if (animation == null) {
            animation = AnimationUtils.loadAnimation( context, R.anim.toast_rotate_infinite );
        }
        toastView.setAnimation( animation );
        toast.setDuration( Toast.LENGTH_SHORT );
        toast.setGravity( Gravity.BOTTOM, 0, 150 );
        toast.show();
        toastView.startAnimation( animation );
    }

    public static void showToast(Context context, int msgId) {
        showToast( context, context.getString( msgId ) );
    }

}
